﻿namespace Blue_Rogue_Remote
{
    /// <summary>
    /// Controller for setting speed and direction of robot
    /// </summary>
    public class RobotController
    {
        #region fields
        private BTController _bt;
        private Tools _tools;
        private byte _dataByte;
        #endregion

        #region properties
        /// <summary>
        /// Gets or Sets COM port in use
        /// </summary>
        public string PortName
        { 
            get { return _bt.Port; }
            set { _bt.Port = value; }
        }

        /// <summary>
        /// Get last send byte
        /// </summary>
        public byte LastSendDataByte { get { return _dataByte; } }
        #endregion

        #region ctor
        /// <summary>
        /// Ctor
        /// </summary>
        public RobotController()
        {
            _bt = new BTController();
            _tools = new Tools();

        }
        #endregion

        #region members
        /// <summary>
        /// Get all availible COM portnames
        /// </summary>
        /// <returns></returns>
        public string[] GetPortNames()
        {
            return _bt.GetPortNames();

        }
        /// <summary>
        /// Creates a movement-byte for controlling Rogue Robot via BT
        /// and sends the byte to the robot
        /// </summary>
        /// <param name="turn">-7 to 7</param>
        /// <param name="speed">-7 to 7</param>
        public void MoveRobot(int turnrate, int speed)
        {
            _dataByte = _tools.MovementToByte(-turnrate, speed);

            //Send data to robot
            _bt.Send(_dataByte);

        }

        /// <summary>
        /// Connect to robot
        /// </summary>
        /// <returns>True if connected</returns>
        public bool ConnectBlueTooth()
        {
            return _bt.Connect();
        }

        /// <summary>
        /// Disconnect from robot
        /// </summary>
        /// <returns>true if disconnected</returns>
        public bool DisconnectBlueTooth()
        {
            return _bt.Disconnect();
        }
        #endregion
    }
}
